package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.menggudf.entity.PartsCoordinate;

@Repository
public class PartsCoordinateDao{

    private static final Log log = LogFactory.getLog(PartsCoordinateDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(PartsCoordinate transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(PartsCoordinate persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public PartsCoordinate merge(PartsCoordinate detachedInstance) {
        try {
            PartsCoordinate result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public PartsCoordinate findById(Long id) {
        try {
            PartsCoordinate instance = entityManager.find(PartsCoordinate.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public PartsCoordinate findByPart(String part) {
        try {
            Query query = entityManager.createQuery(" from PartsCoordinate p where p.parts=?1 ").setParameter(1, part);
            List<PartsCoordinate> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("findByPart fail",e);
            throw e;
        }
    }

    public List<PartsCoordinate> getAllCoordinate(String sexflag, String prosAndCons) {
        try {
            StringBuffer sqld = new StringBuffer("from PartsCoordinate p where p.sexflag=?1 and p.prosAndCons=?2");
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, sexflag).setParameter(2, prosAndCons);
            return query.getResultList();
        }catch(Exception e){
            log.error("getAllCoordinate fail",e);
            throw e;
        }
    }

    public PartsCoordinate findByCoordinate(String coordinate_X, String coordinate_Y,String sexflag, String prosAndCons) {
        try {
            Query query = entityManager.createQuery(" from PartsCoordinate p where p.coordinate_X=?1 and p.coordinate_Y=?2 and p.sexflag=?3 and p.prosAndCons=?4")
                    .setParameter(1, coordinate_X).setParameter(2, coordinate_Y).setParameter(3, sexflag).setParameter(4, prosAndCons);
            List<PartsCoordinate> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("findByPart fail",e);
            throw e;
        }
    }
}
